package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;
import cn.kooki.app.duobao.data.Bean.User.win.WinItemInfo;
import cn.kooki.app.duobao.util.SharePreferencesUtils;
import cn.kooki.app.duobao.util.TimeUtils;

/**
 * Created by xiong on 2015/12/22.
 */
public class RecordAdapter extends BaseAdapter {


    private Context context;
    private ArrayList<WinItemInfo> datas;
    private UserInfo userInfo;

    public interface WinShareClickListener {
        void onShareClick(int position);
    }

    private WinShareClickListener winShareClickListener;

    public void setWinShareClickListener(WinShareClickListener winShareClickListener) {
        this.winShareClickListener = winShareClickListener;
    }

    public RecordAdapter(Context context, ArrayList<WinItemInfo> datas) {
        this.context = context;
        this.datas = datas;
        userInfo = SharePreferencesUtils.getUserinfo(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        WinItemInfo winItemInfo = datas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_mywinrecord, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.goodName.setText(winItemInfo.getTitle());
        viewHolder.goodTenLabel.setVisibility(winItemInfo.getYunjiage().equals("10") ? View.VISIBLE : View.GONE);
        viewHolder.goodsPeriod.setText("参与期号 : " + winItemInfo.getQishu());
        viewHolder.goodInfo.setText("总需 : " + winItemInfo.getZongrenshu() + "人次");
        String lucky = "幸运号码 : " + winItemInfo.getQ_user_code();
        SpannableStringBuilder builder = new SpannableStringBuilder(lucky);
        builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.main_red)), 7, lucky.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.luckycode.setText(builder);
        viewHolder.total.setText("本期参与 : " + winItemInfo.getGonum());
        viewHolder.wintime.setText("揭晓时间 : " + TimeUtils.getTime(winItemInfo.getQ_end_time()));

        Glide.with(context).load(winItemInfo.getThumb()).into(viewHolder.goodImage);
        if (userInfo.getUid().equals(winItemInfo.getQ_uid())) {
            viewHolder.statusWrapper.setVisibility(View.VISIBLE);
            switch (winItemInfo.getStatus()) {
                case 1:
                    viewHolder.status.setText("等待奖品派发");
                    break;
                case 2:
                    viewHolder.status.setText("奖品正在物流中");
                    break;
                case 3:
                    viewHolder.status.setText("奖品已签收");
                    break;
            }

            viewHolder.status.setVisibility(View.VISIBLE);
            viewHolder.button.setVisibility(View.VISIBLE);
            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (winShareClickListener != null) {
                        winShareClickListener.onShareClick(position);
                    }
                }
            });
        } else {
            viewHolder.statusWrapper.setVisibility(View.GONE);
        }
        viewHolder.goodTenLabel.setVisibility(winItemInfo.getYunjiage().equals("10") ? View.VISIBLE : View.GONE);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_mywinrecord.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.good_image)
        ImageView goodImage;
        @Bind(R.id.good_name)
        TextView goodName;
        @Bind(R.id.good_info)
        TextView goodInfo;
        @Bind(R.id.goods_period)
        TextView goodsPeriod;
        @Bind(R.id.luckycode)
        TextView luckycode;
        @Bind(R.id.total)
        TextView total;
        @Bind(R.id.wintime)
        TextView wintime;
        @Bind(R.id.button)
        Button button;
        @Bind(R.id.status)
        TextView status;
        @Bind(R.id.status_wrapper)
        LinearLayout statusWrapper;
        @Bind(R.id.goods_free_label)
        ImageView goodsFreeLabel;
        @Bind(R.id.good_ten_label)
        ImageView goodTenLabel;
        @Bind(R.id.limit_duobao_id)
        ImageView limitDuobaoId;
        @Bind(R.id.custom_top_left_icon)
        ImageView customTopLeftIcon;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
